package com.onLineCar.manages.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.onLineCar.common.core.domain.Result;
import com.onLineCar.manages.pojo.VehicleLevel;
import com.onLineCar.manages.pojo.dto.VehicleLevelDTO;
import com.onLineCar.manages.pojo.request.CarLevelRequest;
import com.onLineCar.manages.service.VehicleLevelService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.onLineCar.manages.aspect.config.OperateLogAspect.userNameThreadLocal;

/**
 * <p>
 * 车辆级别表 前端控制器
 * </p>
 *
 * @author zhucaiyi
 * @since 2023-12-10
 */
@RestController
@RequestMapping("/vehicle-level")
public class VehicleLevelController {

    @Resource
    private VehicleLevelService levelService;


    /**
     * 车辆级别列表
     *
     * @return
     */
    @GetMapping("/queryCarLevel")
    public Result<IPage<VehicleLevel>> queryCarLevel(CarLevelRequest carLevelRequest) {
        return Result.success(levelService.queryCarLevel(carLevelRequest));
    }

    /**
     * 车辆级别全量
     *
     * @return
     */
    @GetMapping("/queryCarLevelAll")
    public Result<List<VehicleLevel>> queryCarLevelAll() {
        return Result.success(levelService.list(new LambdaQueryWrapper<VehicleLevel>()
                .eq(VehicleLevel::getStatus, NumberUtils.INTEGER_ZERO)));
    }

    /**
     * 根据id查询车辆级别
     *
     * @param id
     * @return
     */
    @GetMapping("/queryCarLevelById/{id}")
    public Result<VehicleLevel> queryCarLevelById(@PathVariable("id") Long id) {
        return Result.success(levelService.queryCarLevelById(id));
    }


    /**
     * 添加车辆级别
     *
     * @return
     */
    @PostMapping("/addCarLevel")
    public Result<Boolean> addCarLevel(@Valid @RequestBody VehicleLevelDTO vehicleLevelDTO) {
        return Result.success(levelService.addCarLevel(vehicleLevelDTO.convertVehicleLevel(vehicleLevelDTO, null)));
    }

    /**
     * 编辑车辆级别
     *
     * @param vehicleLevelDTO
     * @return
     */
    @PostMapping("/updateCarLevel")
    public Result<Boolean> updateCarLevel(@Valid @RequestBody VehicleLevelDTO vehicleLevelDTO) {
        return Result.success(levelService.UpdateCarLevel(vehicleLevelDTO.convertVehicleLevel(vehicleLevelDTO, userNameThreadLocal.get())));
    }


    /**
     * 删除车辆级别
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCarLevelById/{id}")
    public Result<Boolean> deleteCarLevelById(@PathVariable("id") Long id) {
        return Result.success(levelService.deleteCarLevelById(id));
    }

}

